package org.example.domain.esdomain;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.domain.User;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户索引库操作实体
 */
@Data
@Document(indexName = "user")
@NoArgsConstructor
public class UserIndex implements Serializable {

    /**
     * 主键id
     */
    @Id
    private Long id;

    /**
     * 用户头像
     */
    @Field(type = FieldType.Keyword, index = false)
    private String headImage;

    /**
     * 账号
     */
    @Field(type = FieldType.Keyword)
    private String account;

    /**
     * 用户密码
     */
    @Field(type = FieldType.Keyword, index = false)
    private String password;

    /**
     * 名称
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart")
    private String userName;

    /**
     * 昵称
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart")
    private String nickName;

    /**
     * 电话号码
     */
    @Field(type = FieldType.Keyword)
    private String phone;

    /**
     * 电子邮箱
     */
    @Field(type = FieldType.Keyword)
    private String email;

    /**
     * 性别
     */
    @Field(type = FieldType.Integer)
    private Integer sex = 1;

    /**
     * 出生日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(type = FieldType.Date, index = false, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date birthday;

    /**
     * 家庭住址
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart")
    private String address;

    /**
     * 详细地址
     */
    @Field(type = FieldType.Keyword, index = false)
    private String addressDetail;

    /**
     * 登录次数
     */
    @Field(type = FieldType.Integer, index = false)
    private Integer loginNum = 0;

    /**
     * 登录时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date loginTime;

    /**
     * 用户标识（0：商家，1：用户,2:管理员）
     */
    @Field(type = FieldType.Integer)
    private Integer identifying;

    /**
     * 状态（0：未登录，1：已登录，2：已注销）
     */
    @Field(type = FieldType.Integer)
    private Integer status;

    /**
     * 创建人
     */
    @Field(type = FieldType.Keyword)
    private String createUser;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(type = FieldType.Date, index = false, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 更新人
     */
    @Field(type = FieldType.Keyword)
    private String updateUser;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field(type = FieldType.Date, index = false, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 删除标识
     */
    @Field(type = FieldType.Integer)
    private Integer delFlag = 0;

    public UserIndex(User user) {
        this.id = Long.valueOf(user.getId());
        this.headImage = user.getHeadImage();
        this.account = user.getAccount();
        this.password = user.getPassword();
        this.userName = user.getUserName();
        this.nickName = user.getNickName();
        this.phone = user.getPhone();
        this.email = user.getEmail();
        this.sex = user.getSex();
        this.birthday = user.getBirthday();
        this.address = user.getAddress();
        this.addressDetail = user.getAddressDetail();
        this.loginNum = user.getLoginNum();
        this.loginTime = user.getLoginTime();
        this.identifying = user.getIdentifying();
        this.status = user.getStatus();
        this.createTime = user.getCreateTime();
        this.createUser = user.getCreateUser();
        this.updateTime = user.getUpdateTime();
        this.updateUser = user.getUpdateUser();
        this.delFlag = user.getDelFlag();
    }
}
